package com.rong.juc_test.juc_base.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程间定制化通信
 * 启动三个线程：a线程打印5次，b线程打印10次，c线程打印15次 进行10轮
 */

class ShareResource{
    // 定义标识位
    private int flag = 1;

    private Lock lock = new ReentrantLock();
    // 创建三个condition
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();


    // 打印5次 参数第几轮
    public void print5(int loop) throws InterruptedException {
        lock.lock();
        try {
            while (flag != 1){
                condition1.await();
            }
            for (int i = 1; i <= 5; i++){

                System.out.println(Thread.currentThread().getName() + ": i= " + i + "-第" + loop + "轮");
            }
            // 通知
            flag = 2;
            condition2.signal();
        }finally {
            lock.unlock();
        }
    }

    // 打印10次 参数第几轮
    public void print10(int loop) throws InterruptedException {
        lock.lock();
        try {
            while (flag != 2){
                condition2.await();
            }
            for (int i = 1; i <= 10; i++){
                System.out.println(Thread.currentThread().getName() + ": i= " + i + "-第" + loop + "轮");
            }
            // 通知
            flag = 3;
            condition3.signal();
        }finally {
            lock.unlock();
        }
    }

    // 打印10次 参数第几轮
    public void print15(int loop) throws InterruptedException {
        lock.lock();
        try {
            while (flag != 3){
                condition3.await();
            }
            for (int i = 1; i <= 15; i++){
                System.out.println(Thread.currentThread().getName() + ": i= " + i + "-第" + loop + "轮");
            }
            // 通知
            flag = 1;
            condition1.signal();
        }finally {
            lock.unlock();
        }
    }
}

public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++){
                try {
                    shareResource.print5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"a").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++){
                try {
                    shareResource.print10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"b").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++){
                try {
                    shareResource.print15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"c").start();
    }

}
